<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of newForum
 *
 * @author ivan
 */
include_once 'function.php';


class newReply {
    //put your code here
    
    
    
    private $topicTitle;
    private $contentOfReply;
    private $Db;
    private $topicID;
    private $forumId1;
    private $replyID;
            
    
    
    
    public function __construct($topicTitle,$reply, $replyID) {
        
        
        $reply = htmlspecialchars($reply);
        $topicTitle = htmlspecialchars($topicTitle);
        $replyID = htmlspecialchars($replyID);
        
        $this->contentOfReply = $reply;
        $this->topicTitle = $topicTitle;
        $this->replyID = $replyID;
        $this->Db = Db();
        
    }
    
    
    public function check(){
        
       
            
            if(strlen($this->contentOfReply) > 0){
                
                return "ok";
                
            }
            else{
                
                $error = "Sorry, your reply cannot be empty";
                return $error;
            }
            
        
        
    }
    
    
    
    public function add(){
        
        
         
         
          $query = $this->Db->prepare('SELECT * FROM topic where topicTitle = :topicTitle');
            $query->execute(array(
            'topicTitle' => $this->topicTitle,
        ));
            
           $response = $query->fetch();
           
           $this->topicID = $response['tid'];
         
         
         
         //Inserting the reply into the reply table
         $query = $this->Db->prepare('INSERT INTO reply(contentOfReply,dateCreated,dateUpdated,topicID,owner,replyID) values(:contentOfReply,NOW(),NOW(),:topicID,:owner,:replyID)');
         $query->execute(array('contentOfReply' => $this->contentOfReply,'topicID' => $this->topicID,'owner' => $_SESSION['id'],'replyID'=>$this->replyID));
         
         //updating the number of replies in the topic table
          $query = $this->Db->prepare('update topic SET numberOfReplies = numberOfReplies + 1,lastUpdatedBy = :lastUpdatedBy, dateUpdated = NOW() where tid = :topicID');
        
         $query->execute(array(
             'lastUpdatedBy' => $_SESSION['id'],
             'topicID' => $this->topicID,     
        ));
         
         
         //extracting the forum associated to the reply to update its number of replies
         
         $query = $this->Db->prepare('SELECT *from topic where topicTitle = :topicTitle');
         $query->execute(array(
            'topicTitle' => $this->topicTitle,
        ));
         
         $response = $query->fetch();
         
         $this->forumId1 = $response['forumID'];
         
         //updating the number of replies in the forum associated to the topic      
        $query = $this->Db->prepare('update forum SET numberOfReplies = numberOfReplies + 1,lastUpdatedBy = :lastUpdatedBy, dateCreated = NOW() where fid = :forumId1');
        
         $query->execute(array(
             'lastUpdatedBy' => $_SESSION['id'],
             'forumId1' => $this->forumId1,     
        ));
         
         
       
        return 1;
    }
}
